//
//  SegmentHeaderView.m
//  PigMarket
//
//  Created by mac on 2019/5/4.
//  Copyright © 2019 felix.pigmarket.oc. All rights reserved.
//

#import "MapSearchResultView.h"
#import "TribeRangeCell.h"

@interface MapSearchResultView ()<UITableViewDelegate,UITableViewDataSource>
/**
 myTableView
 */
@property (nonatomic , strong) UITableView *myTableView;
/**
 resultArr
 */
@property (nonatomic , strong) NSMutableArray *resultArr;
@end
static MapSearchResultView *instance = nil;
@implementation MapSearchResultView

#pragma mark - init method

+ (instancetype)sharedInstance {
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        instance = [[MapSearchResultView alloc] init];
    });
    return instance;
}
- (instancetype)init {
    self = [super init];
    if (self) {
        self.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:0.5];
    }
    return self;
}
#pragma mark - delegate
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.resultArr.count;
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return 0;
}
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    return 0;
}
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    return [[UIView alloc] init];
}
- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section {
    return [[UIView alloc] init];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    TribeRangeCell *cell = [TribeRangeCell cellWithTableView:tableView];
    if (self.resultArr.count > indexPath.row) {
        UserSearchAddressResult *model = self.resultArr[indexPath.row];
        [cell configUserSearchCellWithSearchResultModel:model];
    }
    return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    if (self.resultArr.count > 0) {
        UserSearchAddressResult *model = self.resultArr[indexPath.row];
        if (model.isInside && self.userSearchDidSelectCellBlock) {
            self.userSearchDidSelectCellBlock(model);
        }
    }
}
#pragma mark - private method
- (void)showSearchResultViewWithFrame:(CGRect)frame {
    self.frame = frame;
    self.alpha = 0;
    [UIView animateWithDuration:0.3 animations:^{
         self.alpha = 1;
    } completion:^(BOOL finished) {
        [[UIApplication sharedApplication].keyWindow addSubview:self];
        [self.resultArr removeAllObjects];
        [self addSubview:self.myTableView];
    }];
}
- (void)configTableViewWithDataArr:(NSMutableArray *)dataArr completeBlock:(void (^)(UserSearchAddressResult *))completeBlock {
    self.alpha = 1;
    [self.resultArr removeAllObjects];
    [self.resultArr addObjectsFromArray:dataArr];
    [self.myTableView reloadData];
    self.userSearchDidSelectCellBlock = completeBlock;
}

- (void)hide {
   
    [UIView animateWithDuration:0.3 animations:^{
        self.alpha = 0.0;
    } completion:^(BOOL finished) {
        [self.resultArr removeAllObjects];
        [self.myTableView reloadData];
        [[UIApplication sharedApplication].keyWindow removeFromSuperview];
    }];
}

- (void)removeSearchData {
    [self.resultArr removeAllObjects];
    [self.myTableView reloadData];
}
#pragma mark - getter setter

- (UITableView *)myTableView {
    if (!_myTableView) {
        _myTableView = [[UITableView alloc] initWithFrame:self.bounds style:UITableViewStyleGrouped];
        _myTableView.delegate = self;
        _myTableView.dataSource = self;
        _myTableView.rowHeight = 68;
        _myTableView.scrollEnabled = NO;
        _myTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        _myTableView.backgroundColor = [[UIColor whiteColor] colorWithAlphaComponent:0.0];
    }
    return _myTableView;
}
- (NSMutableArray *)resultArr {
    if (!_resultArr) {
        _resultArr = [[NSMutableArray alloc] init];
    }
    return _resultArr;
}
@end
